package com.example.rjxmgl.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author Chopper
 */
@Slf4j
@Configuration
//@EnableSwagger2WebMvc
public class Swagger2Config {

    @Value("${swagger.title}")
    private String title;

    @Value("${swagger.description}")
    private String description;

    @Value("${swagger.version}")
    private String version;

    @Value("${swagger.termsOfServiceUrl}")
    private String termsOfServiceUrl;

    @Value("${swagger.contact.name}")
    private String name;

    @Value("${swagger.contact.url}")
    private String url;


    private static final String headerName = "accessToken";//请求头名称
    @Bean
    public OpenAPI defaultOpenAPI() {
        Components components = new Components();
        //添加右上角的统一安全认证
        components.addSecuritySchemes(headerName,
                new SecurityScheme()
                        .type(SecurityScheme.Type.APIKEY)
                        .scheme("basic")
                        .name(headerName)
                        .in(SecurityScheme.In.HEADER)
                        .description("请求头")
        );

        return new OpenAPI()
                .info(info())
                .externalDocs(documentation())
                .addSecurityItem(new SecurityRequirement().addList("Authorization"))
                .components(components);

    }

    public Info info() {
        return new Info()
                .title(title)
                .version(version)
                .description(description)
                .license(new License().name("许可证名称").url("许可证地址"))
                .contact(new Contact().name(name))
                .summary("概要");
    }
    public ExternalDocumentation documentation() {
        return new ExternalDocumentation().description("文档描述").url("文档地址");
    }

    @Bean
    public GroupedOpenApi functionApi() {
        return GroupedOpenApi.builder()
                .displayName("功能实现")
                .group("function")
                .addOperationCustomizer((operation, handlerMethod) -> {
                    operation.addSecurityItem(new SecurityRequirement().addList(headerName));
                    return operation;
                })
                //这里原先的包是com.wteam.controller.passport
                //client-api包下的controller
                .packagesToScan("com.example.rjxmgl.controller")
                //.packagesToScan("com.wteam.framework.modules.staff.controller")
                .build();
    }


}
